package by.bsu.fpmi.bbtp.dao;

import by.bsu.fpmi.bbtp.entities.Client;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class ClientDAO extends AbstractDAO<Client> {

    @Override
    protected Class<Client> getClassOfObject() {
        return Client.class;
    }

    @Override
    public void save(Client object) {
        object.setEmail(object.getEmail().toLowerCase());
        super.save(object);
    }

    public Client findByEmail(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Client.class);
        criteria.add(Restrictions.eq("email", email.toLowerCase()));
        List<Client> users = getHibernateTemplate().findByCriteria(criteria);
        if (users.isEmpty()) {
            return null;
        } else {
            return users.get(0);
        }
    }
}
